import Vue from 'vue'
import App from './App'

Vue.config.productionTip = false
App.mpType = 'app'
var urlEncode=function(param, key, encode) {
    if (param==null) return '';
    var paramStr = '';
    var t = typeof (param);
    if (t == 'string' || t == 'number' || t == 'boolean') {
        paramStr += '&' + key + '='  + ((encode==null||encode) ? encodeURIComponent(param) : param); 
    } else {
        for (var i in param) {
            var k = key == null ? i : key + (param instanceof Array ? '[' + i + ']' : '.' + i)
            paramStr += urlEncode(param[i], k, encode)
        }
    }
    return paramStr;
}
Vue.prototype.ajax=function(url,data,callback){
	uni.request({
		url:'https://demo.yufeinong.com/miniapp'+url,
		method:"POST",
		data:data,
		success:function(res) {
			uni.hideLoading();
			if(res.data.code==1||res.data.code==2){
				if(callback!=null){
					callback(res);
				}
				
			}else if(res.data.code==403){
				uni.showToast({
					title:res.data.msg,
					icon:"none"
				});
				setTimeout(function(){
					uni.removeStorageSync("token");
					var pages = getCurrentPages() //获取加载的页面
					var currentPage = pages[pages.length-1] //获取当前页面的对象
					var url = "/"+currentPage.route //当前页面url
					var params=currentPage.options;
					var paramStr="";
					if(params.length>2){
						paramStr="?"+urlEncode(params).slice(1);
					}
					console.log(url);
					if(url!="/pages/login/login"){
						console.log("跳转到登录页");
						uni.navigateTo({
							url:"/pages/login/login?from="+encodeURIComponent(url+paramStr),
						})
					}
					
				},1000);
			}else{
				uni.showToast({
					title:res.data.msg,
					icon:"none"
				});
				
			}
			
		},
		fail:function(){
			uni.hideLoading();
			uni.showToast({
				title:"网络错误",
				icon:"none"
			});
		}
	})
}
const app = new Vue({
    ...App
})
app.$mount()
